package cn.udesk.web.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.udesk.common.toolkit.Constant;
import cn.udesk.core.dto.BootTablePage;
import cn.udesk.core.dto.ResultDto;
import cn.udesk.core.model.KUser;
import cn.udesk.web.service.TransRecordService;
import cn.udesk.web.utils.JsonUtils;

@RestController
@RequestMapping("/trans/record/")
public class TransRecordController {

  @Autowired
  private TransRecordService transRecordService;

  @RequestMapping("getList.shtml")
  public String getList(Integer offset, Integer limit, Integer transId,
      HttpServletRequest request) {
    KUser kUser = (KUser) request.getSession().getAttribute(Constant.SESSION_ID);
    BootTablePage list = transRecordService.getList(offset, limit, kUser.getUId(), transId);
    return JsonUtils.objectToJson(list);
  }

  @RequestMapping("getLogContent.shtml")
  public String getLogContent(Integer recordId) {
    try {
      String logContent = transRecordService.getLogContent(recordId);
      return ResultDto.success(logContent.replace("\n", "<br/>"));
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }
}
